#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <signal.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define BUFLEN 512
#define PORT 36666
#define SRV_IP "10.255.255.249"
static int count = 0;
static struct itimerval oldtv;
struct sockaddr_in si_other;
int s;
char buf[BUFLEN];

void diep(char *s)
{
	perror(s);
	exit(1);
}



void set_timer()
{
	struct itimerval itv;
	itv.it_interval.tv_sec = 1;
	itv.it_interval.tv_usec = 0;
	itv.it_value.tv_sec = 1;
	itv.it_value.tv_usec = 0;
	setitimer(ITIMER_REAL, &itv, &oldtv);
}

void signal_handler(int m)
{
        int len;
	count ++;
	printf("%d\n", count);
        printf("Sending packet %d\n", count);
        sprintf(buf, "This is packet %d\n", count);
        len = sizeof( struct sockaddr);
        if (sendto(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other,len )==-1)
        diep("sendto()");
}


int main(void)
{
  //struct sockaddr_in si_other;
  int  i, slen=sizeof(si_other);
  int ilen = 0;
  //char buf[BUFLEN];

  if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
    diep("socket");

  //memset(&si_other, 0, sizeof(si_other));
  bzero(&si_other, sizeof(struct sockaddr_in));
  si_other.sin_family = AF_INET;
  si_other.sin_port = htons(PORT);
  if (inet_aton(SRV_IP, &si_other.sin_addr)==0) 
  {
  
    fprintf(stderr, "inet_aton( failed\n");
    exit(1);
  }
  signal(SIGALRM, signal_handler);
  set_timer();
  while(count < 10000)
  {
  }

  close(s);
  return 0;
}
